using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;

using Microsoft.Win32;

namespace DslEditorPowerToy.Installers
{
    /// <summary>
    /// Resets the specified hive of VS
    /// </summary>
    [RunInstaller(false)]
    public partial class ResetVsInstaller : Installer
    {
        private const string DevEnv_ProcessName = @"devenv.exe";
        private const string DevEnv_Executable = @"devenv.com";
        private const string DevEnv_CommandLineFormat = @"/rt {0}/setup";

        #region Constructors
        public ResetVsInstaller() : base()
        {
        }
        #endregion

        #region Public Methods
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

            //Ensure VS is not running
            CheckDevEnv();
        }
        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);

            //Ensure VS is not running
            CheckDevEnv();

            //Build path to "devenv.com"
            string executablePath = base.Context.Parameters["DevEnvPath"];
            if (true == string.IsNullOrEmpty(executablePath))
                throw new InvalidOperationException(Resources.DevEnvPathInvalid);
            executablePath = Path.Combine(executablePath, DevEnv_Executable);

            //Build command line parameters
            string hive = base.Context.Parameters["Hive"];
            string commandLine = string.Empty;
            if (false == string.IsNullOrEmpty(hive))
                commandLine = string.Format(DevEnv_CommandLineFormat, string.Format("\"{0}\" ", hive));
            else
                commandLine = string.Format(DevEnv_CommandLineFormat, string.Empty);

            //Run process
            Process devenvProcess = new Process();
            //devenvProcess.StartInfo.WorkingDirectory = executablePath;
            devenvProcess.StartInfo.FileName = executablePath;
            devenvProcess.StartInfo.Arguments = commandLine;
            devenvProcess.StartInfo.UseShellExecute = false;
            devenvProcess.StartInfo.RedirectStandardInput = true;
            devenvProcess.StartInfo.RedirectStandardOutput = true;
            if (true == devenvProcess.Start())
                devenvProcess.WaitForExit();
        }
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);

            //Ensure VS is not running
            CheckDevEnv();
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Ensures that no instances of VS are running
        /// </summary>
        private void CheckDevEnv()
        {
            //Ensure not in debug mode
            if ((false == base.Context.Parameters.ContainsKey("DesignMode")
                && (Process.GetProcessesByName(DevEnv_ProcessName).Length != 0)))
                throw new InvalidOperationException(Resources.VisualStudioProcessRunning);
        }
        #endregion
    }
}